package com.example.myfirstapplication.dbHelper

import android.content.Context
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.util.Log
import android.widget.Toast
import com.example.myfirstapplication.utils.Tag

class MyDatabaseHelper(val context: Context, name: String, version: Int) :
    SQLiteOpenHelper(context, name, null, version) {
    private val createTableStudent = "CREATE TABLE \"student\" (" +
            "\"id\" integer NOT NULL PRIMARY KEY AUTOINCREMENT," +
            "\"stu_num\" text," +
            "\"stu_name\" text," +
            "\"stu_gender\" text," +
            "\"stu_age\" integer );"
    private val createTableScore = "CREATE TABLE \"score\" (" +
            "\"id\" integer NOT NULL PRIMARY KEY AUTOINCREMENT," +
            "\"stu_num\" text," +
            "\"subject\" text," +
            "\"score\" text," +
            "\"term\" integer );"

    override fun onCreate(db: SQLiteDatabase) {
        db.execSQL(createTableStudent)
        db.execSQL(createTableScore)
        Toast.makeText(context, "Create succeeded", Toast.LENGTH_SHORT).show()
    }

    override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
        Log.i(Tag, "dbHelper onUpgrade")
        db.execSQL("drop table if exists student")
        db.execSQL("drop table if exists score")
        onCreate(db)
    }
}
